using Tycho.Parser.Tokens;

namespace Tycho.Parser.Peg {
    public class OptionalRule<Context> : Rule<Context> {
        public readonly Rule<Context> SubRule;

        public OptionalRule (Rule<Context> subRule) {
            SubRule = subRule;
        }

        protected override RuleParseResult<Context> ReallyParse (Token[] tokens, int index, Context context) {
            RuleParseResult<Context> result = SubRule.Parse (tokens, index, context);

            if (result == null) {
                return new RuleParseResult<Context> (index, context);
            } else {
                return result;
            }
        }
    }
}